package com.fang.excel.controller;

import java.util.ArrayList;
import java.util.List;

/**
 * 线程不安全实战
 */
public class ArrayListThreadUnsafeExample {

    public static void main(String[] args) throws InterruptedException {

        List<Integer> list = new ArrayList<>();

        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                list.add(i);
            }
        });

        Thread thread2 = new Thread(() -> {
            for (int i = 1000; i < 2000; i++) {
                list.add(i);
            }
        });

        // 启动线程
        thread1.start();
        thread2.start();

        // 等待线程完成
        thread1.join();
        thread2.join();

        // 打印list的大小
        System.out.println("List size: " + list.size());
    }
}
